package org.fit.pdfdom;

import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

@Slf4j
public class D {
    private static Boolean isFirst = true;
    public static int index = 0;

    public static void saveImage(BufferedImage img) throws IOException {
        String savePath = "D://tempOutImg//";

        File dir = new File(savePath);
        if (dir.exists() && isFirst) {
            isFirst = false;
            D.delDir(dir);
        }
        if (!dir.exists()) {
            dir.mkdirs();
        }

        StackTraceElement pre = Thread.currentThread().getStackTrace()[2];
        File imageFile = new File(savePath, "img_" + index++ + ".png");
        System.out.println((String.format("Saved image [%s](%s:%s)", imageFile.getAbsolutePath(), pre.getFileName(), pre.getLineNumber())));
        ImageIO.write(img, "png", imageFile);

    }

    public static boolean delDir(File file) {
        if (file == null) return true;
        if (!file.exists()) {
            return false;
        }

        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File f : files) {
                D.delDir(f);
            }
        }
        return file.delete();
    }

    public static void printInfo(String str, Object obj) {
        log.warn("{}-> {} ({}:{})", str, obj.getClass().getName(), obj.getClass().getSimpleName() + ".java", 1);
    }

    public static void debug(double x, double y, double width, double height) {
        System.out.println("(" + x + "," + y + ")---(" + width + "," + height + ")");
    }
    public static void debug(String str) {
        System.out.println(str);
    }



    public static void comment() {
        StackTraceElement pre = Thread.currentThread().getStackTrace()[2];
        String info = String.format("comment- %s.%s(%s:%d)]", pre.getClass(), pre.getClassName(), pre.getFileName(), pre.getLineNumber());
        System.out.println(info);
    }

    public static void debug(Exception e) {
        System.out.println(e.getMessage());

    }
}
